package com.guyaning.deyson_core.core.ui.refresh;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.guyaning.deyson_core.core.app.Deyson;
import com.guyaning.deyson_core.core.net.RestClient;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.guyaning.deyson_core.core.ui.recycler.DataConverter;
import com.guyaning.deyson_core.core.ui.recycler.MultipleRecycleAdapter;

/**
 * Created by Administrator on 2018/6/5.
 */

public class RefreshHandler implements SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener {

    private final SwipeRefreshLayout REFRESH_LAYOUT;

    private final PagingBean BEAN;

    private final RecyclerView RECYCLERVIEW;

    private MultipleRecycleAdapter mAdapter = null;

    private final DataConverter CONVERTER;

    public RefreshHandler(SwipeRefreshLayout swipeRefreshLayout, RecyclerView recyclerView, DataConverter converter, PagingBean pagingBean) {

        this.REFRESH_LAYOUT = swipeRefreshLayout;

        this.RECYCLERVIEW = recyclerView;

        this.CONVERTER = converter;

        this.BEAN = pagingBean;

        REFRESH_LAYOUT.setOnRefreshListener(this);
    }

    public static RefreshHandler create(SwipeRefreshLayout swipeRefreshLayout, RecyclerView recyclerView,
                                        DataConverter converter) {

        return new RefreshHandler(swipeRefreshLayout, recyclerView, converter, new PagingBean());

    }

    private void refresh() {

        REFRESH_LAYOUT.setRefreshing(true);

        Deyson.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {

                REFRESH_LAYOUT.setRefreshing(false);
            }
        }, 2000);
    }


    public void firstPage(String url, Context context) {

        RestClient.builder()
                .url(url)
                .loader(context)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {

                        final JSONObject jsonObject = JSON.parseObject(response);

                        BEAN.setTotol(jsonObject.getInteger("total"))
                                .setPageSize(jsonObject.getInteger("page_size"));

                        //设置Adapter
                        mAdapter = MultipleRecycleAdapter.create(CONVERTER.setJson(response));

                        mAdapter.setOnLoadMoreListener(RefreshHandler.this,RECYCLERVIEW);

                        RECYCLERVIEW.setAdapter(mAdapter);

                        BEAN.addIndex();

//                        ToastUtils.showShort(response);
                    }
                })
                .build()
                .get();
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {

    }
}
